package com.babel.util.lide;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

/**
 * @author lyj
 * @date 2017年5月23日---上午10:18:21
 */
public class SftpUtil {
    private final static Log logger = LogFactory.getLog(SftpUtil.class);

    /**
     * 从利得文件服务器上下载文件
     * @auth: 2017年5月23日下午2:30:58   LYJ
     */
    public static void downloadSftpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String localPath) throws JSchException {
	Session session = null;
	Channel channel = null;
	JSch jsch = new JSch();
	session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
	session.setPassword(ftpPassword);
	session.setTimeout(100000);
	Properties config = new Properties();
	config.put("StrictHostKeyChecking", "no");
	session.setConfig(config);
	session.connect();

	channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp chSftp = (ChannelSftp) channel;

	try {
	    chSftp.get(ftpPath, localPath);

	    // String directory = "/org/";
	    // chSftp.cd(directory);
	    // chSftp.mkdir("20170528");
	    // chSftp.cd("/org/20170528/");
	    // File file = new File("F:\\OFI_20170522_96.txt");

	    // chSftp.put(new FileInputStream(file), file.getName());
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.info("download error."+e.getMessage());
	} finally {
	    chSftp.quit();
	    channel.disconnect();
	    session.disconnect();
	}

    }

    /**
    * 上传文件到利得服务器
    * @auth: 2017年5月23日下午2:54:27   LYJ
    */
    public static void uploadSftpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String localPath, String fileName) throws JSchException {
	Session session = null;
	Channel channel = null;

	JSch jsch = new JSch();
	session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
	session.setPassword(ftpPassword);
	session.setTimeout(100000);
	Properties config = new Properties();
	config.put("StrictHostKeyChecking", "no");
	session.setConfig(config);
	session.connect();

	channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp chSftp = (ChannelSftp) channel;

	// String ftpFilePath = ftpPath + "/" + fileName;
	String ftpFilePath = "/tag/20170523/OFI_20170523_96.TXT";
	String localFilePath = localPath + File.separatorChar + fileName;

	try {
	    // chSftp.get(ftpFilePath, localFilePath);

	    // String directory = "/org/";
	    // chSftp.cd(directory);
	    // chSftp.mkdir("20170528");
	    chSftp.cd("/org/20170528/");
	    File file = new File("F:\\OFI_20170522_96.txt");

	    chSftp.put(new FileInputStream(file), file.getName());
	} catch (Exception e) {
	    e.printStackTrace();
	    logger.info("download error.");
	} finally {
	    chSftp.quit();
	    channel.disconnect();
	    session.disconnect();
	}

    }
}
